import Vue from "vue";
import VueRouter from "vue-router";

// 导入需要的组件
import Login from '@/components/MyLogin'
import Home from '@/components/MyHome'

import Users from '../components/menus/MyUsers'
import Rights from '../components/menus/MyRights'
import Goods from '../components/menus/MyGoods'
import Orders from '../components/menus/MyOrders'
import Settings from '../components/menus/MySettings'


import Userinfo from '../components/user/MyUserDetail'

Vue.use(VueRouter);

// 定义路由规则
const router = new VueRouter({
  routes:[
    {path:'/',redirect:'/login'},
    {path:'/login',component:Login},
    {path:'/home',
    redirect:'/home/users',
    component:Home,
    children:[
      {path:'users',component:Users},
      {path:'rights',component:Rights},
      {path:'goods',component:Goods},
      {path:'orders',component:Orders},
      {path:'settings',component:Settings},
      {path:'userinfo/:id',component:Userinfo,props:true}
    ]
  }
  ]
});

// 前置守卫
router.beforeEach((to,from,next)=>{
  const pathArr=['/home','/home/users','/home/rights']
  if(pathArr.indexOf(to.path) != -1){
    if(localStorage.getItem('token')){
      next();
    }else{
      alert('未登录，请登录')
      next('/login')
    }
  }else{
    next();
  }
})

export default router;
